package com.styytech.dadabao.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.styytech.dadabao.bean.ProductList;
import com.styytech.dadabao.bean.Rate;
import com.styytech.dadabao.utils.DateUtil;
import com.styytech.dadabao.utils.UiUtils;
import com.styytech.dadabao.bean.Product;
import com.styytech.dadabao.R;

public class ProductAdapter extends BaseAdapter {
	private Context context;
	private ProductHolder myHolder;
	private Product product;
	private List<Product> productList;
	private LayoutInflater mInflater;


	public ProductAdapter(Context paramContext, ProductList paramProductList) {
		this.context = paramContext;
		this.productList = paramProductList.getProductList();
		if(paramContext!=null){
			this.mInflater = LayoutInflater.from(paramContext);
		}
	}

	public void addData(List<Product> paramList) {
		this.productList.addAll(paramList);
		notifyDataSetChanged();
	}

	public int getCount() {
		return this.productList.size();
	}

	public Object getItem(int paramInt) {
		return this.productList.get(paramInt);
	}

	public long getItemId(int paramInt) {
		return paramInt;
	}

	public View getView(int paramInt, View paramView, ViewGroup paramViewGroup) {
		this.product = ((Product) this.productList.get(paramInt));
		if (paramView == null) {
			this.myHolder = new ProductHolder();
			paramView = mInflater.inflate(R.layout.list_item_product, null);
//			paramView = View.inflate(context, R.layout.list_item_product,
//					null);
			this.myHolder.tv_buys = ((TextView) paramView
					.findViewById(R.id.tv_buys));
			this.myHolder.tv_limit_day = ((TextView) paramView
					.findViewById(R.id.tv_limit_day));
			this.myHolder.tv_outof_day = ((TextView) paramView
					.findViewById(R.id.tv_outof_day));
			this.myHolder.tv_plan_info = ((TextView) paramView
					.findViewById(R.id.tv_plan_info));
			this.myHolder.ll_nuber_percent = ((LinearLayout) paramView
					.findViewById(R.id.ll_nuber_percent));
			this.myHolder.ll_year_income = ((LinearLayout) paramView
					.findViewById(R.id.ll_year_income));
			this.myHolder.tv_limit_day_unit = ((TextView) paramView
					.findViewById(R.id.tv_limit_day_unit));
			this.myHolder.tv_outof_day_unit = ((TextView) paramView
					.findViewById(R.id.tv_outof_day_unit));
			this.myHolder.iv_time = ((ImageView) paramView
					.findViewById(R.id.iv_time));
			paramView.setTag(this.myHolder);
		} else {
			this.myHolder = ((ProductHolder) paramView.getTag());
		}

		this.myHolder.tv_plan_info.setText(this.product.getTitle());
		if (1 == this.product.getDate_status()) {
			this.myHolder.iv_time
					.setBackgroundResource(R.drawable.bg_product_sell);
		}else{
			this.myHolder.iv_time
			.setBackgroundResource(R.drawable.bg_product_sold_out);
		}

		this.myHolder.tv_limit_day.setText(this.product.getDate());
		this.myHolder.tv_buys.setText(String.valueOf(this.product.getNumber()));

		this.myHolder.tv_outof_day.setText(DateUtil.getDays(Integer
				.parseInt(this.product.getStoptime())));

		Rate localRate;
		localRate = this.product.getRate();

		UiUtils.rateToString(this.myHolder.ll_nuber_percent, this.context,
				localRate.getMax() + "%");

		this.myHolder.tv_limit_day.setText(this.product.getDate());
		this.myHolder.tv_outof_day.setText(this.product.getStoptime());

		return paramView;
	}

	protected void goToProductDetail() {
	}

	class ProductHolder {
		/** 在售、售罄 */
		ImageView iv_time;
		LinearLayout ll_nuber_percent;
		LinearLayout ll_year_income;
		/** 购买人数 */
		TextView tv_buys;
		TextView tv_limit_day;
		TextView tv_limit_day_unit;
		TextView tv_outof_day;
		TextView tv_outof_day_unit;
		/** 标题 */
		TextView tv_plan_info;

		ProductHolder() {
		}
	}
}